import brandList from '@/components/filter-list/brand.vue'
import carList from './components/carList.vue'
export default {
  components: {
    brandList,
    carList
  },
  data() {
    return {
      isHeight: 0,
      navHight: 0,
      isDefault: false,
      isShow: true,
      sortTitle: '排序',
      priceTitle: '价格',
      carTitle: '购车类型',

      loadStatus: 'loading',
      // 面包屑
      history: {
			  sortP: '',
			  price: '',
			  type: '',
			  brand: '',
      },
      isOpen: false,//是否打开下拉
      sortValue: 0,
      typeValue: 0,
      priceValue: 0,
      carlist: [],
      //排序
      sort: [
        {
				  id: 0,
				  name: '智能排序'
        },
        {
				  id: 1,
				  name: '销量最高'
        },
        {
				  id: 2,
				  name: '距离最近'
        },
        {
				  id: 3,
				  name: '关注最高'
        },
        {
				  id: 4,
				  name: '7迈自营'
        },
      ],
      //价格
      price: [
        {
				  id: 1,
				  name: '10万以下',
          min: 0,
          max: 99999
        },
        {
				  id: 2,
				  name: '10-15万',
          min: 100000,
          max: 150000
        },
        {
				  id: 3,
				  name: '15-20万',
          min: 150000,
          max: 200000
        },
        {
				  id: 4,
				  name: '20-30万',
          min: 200000,
          max: 300000
        },
        {
				  id: 5,
				  name: '30万以上',
          min: 300000,
          max: 999999999
        },
      ],
      //购车类型
      type: [
        {
				  id: 1,
				  name: '一成首付'
        },
        {
				  id: 2,
				  name: '以租代售'
        },
        {
				  id: 3,
				  name: '巨能降'
        }
      ],
      carIndex: 1,//当前页
      brandId: '',//品牌id
      priceMin: 0,//最低价格
      priceMax: 999999999,//最高价格
      list: {},
      pageLoading: false,//页面loading
    }
  },
  onLoad(op) {
    //判断上个页面有木有传过来数据
    if(!op.list)return false
    this.list = JSON.parse(op.list)

    // 如果有再执行各自方法
    if(this.list.price){
		  this.priceMin = this.list.min
		  this.priceMax = this.list.max
		  this.history.price = this.list.price
    }
    if(this.list.Brand){
		  this.brandId = this.list.Bid
		  this.history.brand = this.list.Brand
    }
  },
  computed: {
	  stickyHeight() {
	    return Math.floor(Number(this.navHight) / (uni.upx2px(Number(this.navHight)) / Number(this.navHight))) - 1
	  }
  },
  created(){

	  this.buyHome()
  },
  onReachBottom(){
	  this.buyHome()
  },
  // onReady() {
	 //  //获取导航栏高度
	 //  const query = uni.createSelectorQuery().in(this)
	 //  query.select('.nav').boundingClientRect(data => {
	 //    // this.height = data.height
	 //    this.navHight = data.height
	 //  }).exec()
  // },
  onPullDownRefresh (){
	  // this.buyList()
	  this.buyHome()

  },
  methods: {
    more(){
      this.buyHome()
    },

    // 加载前、中、后
    moreStatus(status) {
		  this.loadStatus = status
    },
    //列表数据
    async buyHome(){
      uni.showLoading({
			    title: '加载中',
        mask: true
      })
      this.loadStatus = 'loading'
      let lat = this.$store.state.lat
      let lon = this.$store.state.lon
      try{
        let data = {
          'sorting': this.sortValue,
          'pageIndex': this.carIndex,
          'pageSize': 10,
          'cartype': this.typeValue,
          'lowprice': this.priceMin,
          'highprice': this.priceMax,
          'brandId': String(this.brandId), //品牌id
          'lat': lat,
          'lon': lon
        }
        const res = await this.$api.buyCar.goodsCarSourceConditionsList(data)

        this.carlist = [...this.carlist, ...res.data.list]
        uni.stopPullDownRefresh()
        this.$nextTick(() => {
          this.pageLoading = true
        })
        if(this.carlist.length===0){
          this.isDefault = true
        }
        if (res.data.total <= 10) {
				  this.loadStatus = 'nomore'
        } else {
				  this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        // this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.carIndex += 1 // 当前页加1
      }catch(e){
        uni.stopPullDownRefresh()
        uni.hideLoading()
      }
    },
    //排序
    onSort(e){
      this.sortTitle = e.name
      this.sortValue = e.id
      this.carIndex = 1
      this.carlist = []
      this.isDefault = false
      this.history.sortP = e.name
      this.buyHome()
      this.$refs.paymentSort.close()
    },
    //价格排序
    priceSort(e){
      this.priceTitle = e.name

      this.carIndex = 1
      this.carlist = []
      this.isDefault = false
      this.priceMin = e.min
      this.priceMax = e.max
      this.priceValue = e.id
      this.history.price = e.name
      this.buyHome()
      this.$refs.paymentSort.close()
    },
    //购车类型排序
    typeSort(e){
      this.carTitle = e.name
      this.carIndex = 1
      this.carlist = []
      this.isDefault = false
      this.typeValue = e.id
      this.history.type = e.name
      this.buyHome()
      this.$refs.paymentSort.close()
    },
    closePayment() {
		  this.$refs.paymentSort.close()
    },
    // 通用品牌排序
    brandListChange(id,type) {
		  this.brandId = id
      this.carlist = []
      this.carIndex = 1
      this.isDefault = false
      this.history.brand = type.brand
      this.buyHome()
    },
    //重置
    reset(){
      this.sortTitle = '排序',
      this.priceTitle = '价格'
      this.carTitle = '购车类型',
      this.carlist = []
      this.carIndex = 1
      this.isDefault = false
      this.brandId = ''
      this.typeValue = 0
      this.priceMin = 0
      this.priceMax = 999999999
      this.sortValue = 0
      this.priceValue = 0
      this.history = {
			  sortP: '',
			  brand: '',
			  price: '',
			  type: '',
      }
      this.buyHome()
    },

    //删除面包屑
    delSort() {
      //删除之后恢复默认
      this.sortValue = 0
      this.carlist = []
      this.carIndex = 1
      this.isDefault = false
      this.sortValue = 0
      this.sortTitle = '智能排序'
		  this.history.sortP = ''
      this.buyHome()
    },
    delBrand() {
      //删除之后恢复默认
      this.carlist = []
      this.carIndex = 1
      this.isDefault = false
      this.brandId = ''
		  this.history.brand = ''
      this.buyHome()
    },
    delPrice() {
      //删除之后恢复默认
      this.priceTitle = '价格'
      this.priceValue = 0
      this.carlist = []
      this.isDefault = false
      this.carIndex = 1
      this.priceMin = 0
      this.priceMax = 999999999
      this.history.price = ''
      this.buyHome()
    },
    delType() {
      //删除之后恢复默认
      this.typeValue = 0
      this.carTitle = '购车类型'
      this.carlist = []
      this.carIndex = 1
      this.isDefault = false
      this.typeValue = 0
      this.history.type = ''
      this.buyHome()
    },
    //搜索
    onSearch(e) {

      if (e === '') {
        uni.showToast({
          title: '请输入关键字',
          icon: 'none',
          duration: 1000
        })
        return false
      }

    },
    //下拉关闭
    close(e) {
      this.isOpen = false

    },
    //下拉列表打开
    open(e) {
      this.isOpen = true
    },
  }
}
